<?php

namespace IDock\Insurance\DBIC\Request;

use IDock\Insurance\DBIC\Response\PolicyPaymentResponse;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Visitor\PolicyPaymentVisitor;
use stdClass;

class PolicyPaymentRequest extends AllDBICRequest
{
    protected $route = 'dbic/pay/pay-order/unified';

    protected $response = PolicyPaymentResponse::class;

    protected $urlTip = '支付接口';

    protected $payKey = '';

    protected $machId = '';

    protected $goodDirection = '';

    protected $backCallUrl = '';

    /**
     * @var  PolicyPaymentVisitor $visitor
     */
    protected $visitor;

    public function setPayKey(string $payKey): void
    {
        $this->payKey = $payKey;
    }

    public function setMachId(string $machId): void
    {
        $this->machId = $machId;
    }

    public function setGoodDirection(string $goodDirection): void
    {
        $this->goodDirection = $goodDirection;
    }

    public function setBackCallUrl(string $backCallUrl): void
    {
        $this->backCallUrl = $backCallUrl;
    }


    public function generate(): array
    {
        $data = [
            'ORDERINFO' => [
                'OUTORDERNO' => $this->visitor->argc['OUTORDERNO'] ?? '',
                'MCHID' => $this->machId,
                'NONCESTR' => md5(uniqid(true)),
                'TRADETYPE' => 'WECHAT',
                'TOTALFEE' => (string)(($this->visitor->argc['TOTALFEE'] ?? '0') * 100),
                'TIMEEXPIRE' => date('Y-m-d 00:00:00', strtotime('+1 day')),
                'ATTACH' => 'false',
                'FRONTJUMPFLAG' => '0',
                'FRONTJUMPURL' => "null",
                'BACKCALLFLAG' => $this->backCallUrl ? '1' : '0',
                'BACKCALLURL' => $this->backCallUrl,
            ],
            'CHECKINFO' => new stdClass(),
            'GOODS' => $this->goodsList(),
        ];
        $data['SIGNINFO'] = ['SIGN' => $this->sign($data['ORDERINFO'])];

        return $data;
    }

    /**
     * @param array $data
     * @return string
     */
    private function sign(array $data): string
    {
        $data['KEY'] = $this->payKey;
        $data = urldecode(http_build_query($data));

        $string = urldecode($data);

        $string = strtoupper(md5($string));

        return $string;
    }

    /**
     * 订单列表
     * @return array
     */
    private function goodsList(): array
    {
        $list = [];

        if ($this->visitor->ciPolicyNo) {
            $list[] = [
                'GOODID' => $this->visitor->ciPolicyNo,
                'GOODSOURCE' => '2',
                'GOODDIRECTION' => $this->goodDirection,
                'PAYFEE' => (string)(($this->visitor->argc['ciPremium'] ?? '0') * 100),
            ];
        }

        if ($this->visitor->biPolicyNo) {
            $list[] = [
                'GOODID' => $this->visitor->biPolicyNo,
                'GOODSOURCE' => '2',
                'GOODDIRECTION' => $this->goodDirection,
                'PAYFEE' => (string)(($this->visitor->argc['biPremium'] ?? '0') * 100),
            ];
        }
        //驾意险
        if(!empty($this->visitor->nonCarInquirySingle)) foreach ($this->visitor->nonCarInquirySingle as $val){
            /**@var NonCarInquirySinglePrinter $val */
            $list[] = [
                'GOODID' => $val->inquiryNo,
                'GOODSOURCE' => '1',
                'GOODDIRECTION' => $this->goodDirection,
                'PAYFEE' => (string)round(($val->premium ?? '0') * $val->insuredNum * 100),
            ];
        }
        return $list;
    }

}
